package edu.cqie.service;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * AnnotationPointCut
 *
 * @description:注解开发，实现aop
 * @Author XiaoZhou
 * @Date 2023/10/17 22:00
 */
@Aspect
public class AnnotationPointCut {
    @Pointcut("execution(* edu.cqie.service.StudentService.*(..))")
    private void pt(){}

    @Before("pt()")
    public void before(JoinPoint joinPoint) {
        long start = System.currentTimeMillis();
        System.out.println("=========开始执行:" + joinPoint.getSignature().getName() + "方法===========开始时间为：" + start);
    }


    @After("pt()")
    public void after(JoinPoint joinPoint) {
        long end = System.currentTimeMillis();
        System.out.println("=========结束执行：" + joinPoint.getSignature().getName() + "方法===========结束时间为：" + end);
    }

    @AfterReturning(value = "pt()", returning = "ret")
    public void afterReturning(Object ret) {
        System.out.println("Method return value: " + ret);
    }

    @AfterThrowing(pointcut = "execution(* edu.cqie.service.*.*(..))", throwing = "ex")
    public void logException(Exception ex) {
        System.out.println("捕捉到异常：" + ex.getMessage());
        ex.printStackTrace();
    }

    //在环绕增强中，我们可以给定一个参数，代表我们要获取处理切入的点
    @Around("pt()")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");
        long start = System.currentTimeMillis();
        //执行方法
        Object proceed = jp.proceed();
        long end = System.currentTimeMillis();
        long time = end - start;
        System.out.println("环绕后");
        System.out.println("方法执行了：" + time + "ms");

    }
}
